21MIA1025 - Abhineswari M - cse4076 - Assignment 1¶
In [1]:
pip install opencv-python
Requirement already satisfied: opencv-python in c:\users\abhineswari madan\anaconda\envs\tensorflowgpu\lib\site-packages (4.10.0.84) Requirement already satisfied: numpy>=1.21.2 in c:\users\abhineswari madan\anaconda\envs\tensorflowgpu\lib\site-packages (from opencv-python) (1.26.4) Note: you may need to restart the kernel to use updated packages.
In [2]:
pip install matplotlib
Requirement already satisfied: matplotlib in c:\users\abhineswari madan\anaconda\envs\tensorflowgpu\lib\site-packages (3.9.1) Requirement already satisfied: contourpy>=1.0.1 in c:\users\abhineswari madan\anaconda\envs\tensorflowgpu\lib\site-packages (from matplotlib) (1.2.1) Requirement already satisfied: cycler>=0.10 in c:\users\abhineswari madan\anaconda\envs\tensorflowgpu\lib\site-packages (from matplotlib) (0.12.1) Requirement already satisfied: fonttools>=4.22.0 in c:\users\abhineswari madan\anaconda\envs\tensorflowgpu\lib\site-packages (from matplotlib) (4.53.1) Requirement already satisfied: kiwisolver>=1.3.1 in c:\users\abhineswari madan\anaconda\envs\tensorflowgpu\lib\site-packages (from matplotlib) (1.4.5) Requirement already satisfied: numpy>=1.23 in c:\users\abhineswari madan\anaconda\envs\tensorflowgpu\lib\site-packages (from matplotlib) (1.26.4) Requirement already satisfied: packaging>=20.0 in c:\users\abhineswari madan\anaconda\envs\tensorflowgpu\lib\site-packages (from matplotlib) (24.1) Requirement already satisfied: pillow>=8 in c:\users\abhineswari madan\anaconda\envs\tensorflowgpu\lib\site-packages (from matplotlib) (10.4.0) Requirement already satisfied: pyparsing>=2.3.1 in c:\users\abhineswari madan\anaconda\envs\tensorflowgpu\lib\site-packages (from matplotlib) (3.1.2) Requirement already satisfied: python-dateutil>=2.7 in c:\users\abhineswari madan\anaconda\envs\tensorflowgpu\lib\site-packages (from matplotlib) (2.9.0.post0) Requirement already satisfied: six>=1.5 in c:\users\abhineswari madan\anaconda\envs\tensorflowgpu\lib\site-packages (from python-dateutil>=2.7->matplotlib) (1.16.0) Note: you may need to restart the kernel to use updated packages.
TASK 1: Basic Image statistics and color space conversion¶
1. read the image: load an image using opencv¶
In [3]:
import cv2
import matplotlib.pyplot as plt
img = cv2.imread('pup.jpeg')
plt.imshow(img)
Out[3]:
<matplotlib.image.AxesImage at 0x10f522407c0>
In [4]:
import numpy as np
2. compute the basic statistics: calculate the mean, standard deviation and histogram of each color¶
In [5]:
def compute_statistics(img):
b, g, r = cv2.split(img)
# Calculating the mean and standard deviation
mean_b, std_b = cv2.meanStdDev(b)
mean_g, std_g = cv2.meanStdDev(g)
mean_r, std_r = cv2.meanStdDev(r)
# Calculating the histograms
hist_b, _ = np.histogram(b.ravel(), 256, [0, 256])
hist_g, _ = np.histogram(g.ravel(), 256, [0, 256])
hist_r, _ = np.histogram(r.ravel(), 256, [0, 256])
return {
'B': {'mean': mean_b[0][0], 'std': std_b[0][0], 'hist': hist_b},
'G': {'mean': mean_g[0][0], 'std': std_g[0][0], 'hist': hist_g},
'R': {'mean': mean_r[0][0], 'std': std_r[0][0], 'hist': hist_r}
}
3. convert color spaces: convert the image to HSV and lab color spaces and display the results¶
In [6]:
def convert_color_spaces(img):
# Convert to HSV
hsv = cv2.cvtColor(img, cv2.COLOR_BGR2HSV)
h, s, v = cv2.split(hsv)
# Convert to LAB
lab = cv2.cvtColor(img, cv2.COLOR_BGR2LAB)
l, a, b = cv2.split(lab)
# Display images
plt.figure(figsize=(12, 6))
plt.subplot(2, 3, 1), plt.imshow(img[:, :, ::-1]), plt.title('Original')
plt.subplot(2, 3, 2), plt.imshow(h, cmap='gray'), plt.title('HSV - Hue')
plt.subplot(2, 3, 3), plt.imshow(s, cmap='gray'), plt.title('HSV - Saturation')
plt.subplot(2, 3, 4), plt.imshow(v, cmap='gray'), plt.title('HSV - Value')
plt.subplot(2, 3, 5), plt.imshow(l, cmap='gray'), plt.title('LAB - L')
plt.subplot(2, 3, 6), plt.imshow(a, cmap='gray'), plt.title('LAB - A')
plt.show()
In [7]:
stats = compute_statistics(img)
In [8]:
for color, data in stats.items():
print(f"Color: {color}")
print(f"Mean: {data['mean']:.2f}")
print(f"Standard Deviation: {data['std']:.2f}")
print("Histogram:", data['hist'])
# Convert color spaces and display
convert_color_spaces(img)
Color: B
Mean: 115.08
Standard Deviation: 56.49
Histogram: [ 259 217 220 433 716 649 686 605 660 536 603 512 528 519
520 442 441 439 440 448 458 427 450 474 478 497 474 456
455 479 492 483 512 510 546 564 506 522 506 573 633 570
583 581 536 580 615 561 650 572 568 625 640 671 647 638
694 744 723 710 750 834 825 910 975 1086 1080 1025 1152 1221
1339 1428 1802 2413 2479 3140 2959 2596 2472 2526 2981 3061 3359 3403
3603 4070 3604 3642 3587 3720 3646 3303 3327 3362 3365 3216 3357 3231
3156 3468 3140 3441 3504 3438 3296 3547 2981 2981 3002 2889 3094 2673
2681 2413 2360 2035 2031 1870 1892 1753 1621 1577 1535 1375 1328 1265
1256 1201 1173 1069 1003 978 973 1100 1051 1020 995 985 928 860
879 857 751 735 684 617 644 584 549 552 573 632 474 556
547 499 553 509 495 526 549 576 553 571 571 559 511 527
568 556 541 515 510 577 491 536 549 521 450 611 520 575
539 531 666 509 597 528 469 616 575 704 634 630 594 648
614 549 643 618 637 638 610 600 685 581 626 700 809 805
769 918 815 885 836 835 868 1037 852 1020 1086 960 870 993
962 821 794 710 759 823 863 886 912 1026 1082 1070 1305 1549
1457 998 1638 383 97 57 48 14 15 1 6 1 0 0
0 0 0 0]
Color: G
Mean: 153.58
Standard Deviation: 41.31
Histogram: [ 78 24 26 32 30 27 31 34 25 39 32 43 35 46
40 25 25 22 19 30 31 15 26 26 29 33 24 29
33 28 32 36 37 21 23 27 32 26 37 65 129 279
280 295 223 316 278 358 607 452 392 441 475 489 420 449
394 365 363 405 353 387 375 392 416 374 433 476 475 500
503 510 517 511 460 500 511 510 515 529 512 562 507 544
512 485 508 492 520 495 531 525 482 538 521 544 538 524
516 526 518 531 547 532 566 519 575 554 535 619 574 636
613 648 667 655 767 750 732 792 797 928 1012 1107 1246 1374
1570 1462 2018 2010 2072 2237 2629 2985 3588 3808 4074 3915 4217 3654
3822 3549 3529 3562 3751 3655 3718 3745 3603 4063 3789 4019 4080 4384
4364 4216 4425 4187 4355 4267 4192 4163 4113 4141 4513 4915 4173 3622
3389 2868 2718 2370 2177 1949 1820 1879 1654 1660 1557 1325 1258 1326
1265 1169 1150 1053 1054 1064 1014 885 857 745 787 746 742 707
754 764 760 674 753 753 764 737 838 846 862 750 683 640
697 834 836 851 861 746 829 898 1032 1157 1094 1092 1067 1136
1071 1182 1190 1271 1401 1503 1402 1604 1875 1630 1197 1820 583 291
296 280 197 186 118 98 79 48 49 55 6 3 1 0
0 0 0 0]
Color: R
Mean: 167.16
Standard Deviation: 38.01
Histogram: [ 4 3 2 4 3 5 11 12 8 9 17 21 25 20
22 21 21 26 26 28 15 40 17 24 20 26 24 20
29 16 23 15 24 13 31 25 14 19 16 49 88 48
45 63 100 157 191 146 180 203 201 168 124 136 149 195
184 169 196 156 346 411 299 331 263 330 342 318 317 308
301 289 295 290 277 323 293 338 293 305 307 302 295 338
357 364 366 370 404 358 384 408 452 455 475 451 399 429
418 459 438 444 458 450 456 456 499 467 521 461 509 478
479 531 479 526 522 532 527 545 602 579 570 560 575 688
697 730 764 777 778 842 921 918 1037 1050 1062 1178 1246 1306
1483 1566 1702 1962 1929 2302 2444 2663 2531 2622 2861 3192 3235 2981
3252 3321 3530 3692 4195 4421 4298 4351 4490 4348 4318 4224 5022 4622
5485 5417 5837 6390 5489 5451 5071 4380 4086 3906 3897 3732 3536 3336
3252 2920 2814 2785 3078 2207 1950 1808 1882 1811 1625 1609 1483 1415
1344 1380 1201 1123 1023 1070 980 909 724 804 906 910 824 805
773 835 869 794 856 918 765 815 795 906 1007 955 1075 1079
1201 1087 1251 1312 1376 1445 1776 2032 1990 1584 2181 947 723 693
657 620 564 577 560 597 546 498 409 320 246 153 103 79
20 11 5 10]
In [9]:
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
threshold_value = 100
_, thresh = cv2.threshold(gray, threshold_value, 255, cv2.THRESH_BINARY)
plt.figure(figsize=(10, 5))
plt.subplot(1, 3, 1), plt.imshow(img[:, :, ::-1]), plt.title('Original Image')
plt.subplot(1, 3, 2), plt.imshow(gray), plt.title('Gray Image')
plt.subplot(1, 3, 3), plt.imshow(thresh, cmap='gray'), plt.title('Segmented Image')
plt.show()
TASK 3 - Colour based segmentation
1.read the image: load an image with objects of different colours¶
In [10]:
img2 = cv2.imread('objects.jpg')
#the cv2 by default reads the image as bgr instead od rgb
img2_rgb = cv2.cvtColor(img2, cv2.COLOR_BGR2RGB)
plt.imshow(img2_rgb)
plt.show()
In [11]:
# this is how rgb to hsv works
hsv2 = cv2.cvtColor(img2_rgb, cv2.COLOR_RGB2HSV)
plt.imshow(hsv2)
plt.show()
2.convert to hsv: convert the image to hsv color space¶
In [12]:
# Convert to HSV color space
hsv = cv2.cvtColor(img2_rgb, cv2.COLOR_BGR2HSV)
3.apply color thresholding: use color thresholds to segment objects of different color¶¶
In [13]:
lower_blue = np.array([80, 50, 50])
upper_blue = np.array([120, 255, 255])
lower_green = np.array([40, 50, 50])
upper_green = np.array([90, 255, 255])
# Creating masks for blue and green objects
mask_blue = cv2.inRange(hsv, lower_blue, upper_blue)
mask_green = cv2.inRange(hsv, lower_green, upper_green)
# Appling masks to original image
res_blue = cv2.bitwise_and(img2, img2, mask=mask_blue)
res_green = cv2.bitwise_and(img2, img2, mask=mask_green)
4.display the results: show the original and segmented images¶
In [14]:
plt.figure(figsize=(15, 5))
plt.subplot(1, 2, 1), plt.imshow(img2_rgb[:, :, ::-1]), plt.title('HSV converted image')
plt.subplot(1, 2, 2), plt.imshow(mask_blue, cmap='gray'), plt.title('Blue Mask')
plt.subplot(2, 2, 1), plt.imshow(mask_green, cmap='gray'), plt.title('Green Mask')
plt.subplot(2, 2, 2), plt.imshow(cv2.addWeighted(res_blue, 1, res_green, 1, 0)), plt.title('Segmented Image')
plt.show()
TASK 4- Feature extraction from segmented objects¶
1.read and segment the image: load a image and segment objects based on color or intensity¶
In [15]:
img3 = cv2.imread('multi_object.png')
img3_rgb = cv2.cvtColor(img3, cv2.COLOR_BGR2RGB)
img4 = cv2.imread('multi_object.png')
plt.figure(figsize=(15, 5))
plt.subplot(1, 2, 1), plt.imshow(img3_rgb), plt.title('Original Image')
plt.subplot(1, 2, 2), plt.imshow(img4), plt.title('HSV converted image')
plt.show()
In [16]:
pip install scikit-image
Requirement already satisfied: scikit-image in c:\users\abhineswari madan\anaconda\envs\tensorflowgpu\lib\site-packages (0.24.0) Requirement already satisfied: numpy>=1.23 in c:\users\abhineswari madan\anaconda\envs\tensorflowgpu\lib\site-packages (from scikit-image) (1.26.4) Requirement already satisfied: scipy>=1.9 in c:\users\abhineswari madan\anaconda\envs\tensorflowgpu\lib\site-packages (from scikit-image) (1.14.0) Requirement already satisfied: networkx>=2.8 in c:\users\abhineswari madan\anaconda\envs\tensorflowgpu\lib\site-packages (from scikit-image) (3.3) Requirement already satisfied: pillow>=9.1 in c:\users\abhineswari madan\anaconda\envs\tensorflowgpu\lib\site-packages (from scikit-image) (10.4.0) Requirement already satisfied: imageio>=2.33 in c:\users\abhineswari madan\anaconda\envs\tensorflowgpu\lib\site-packages (from scikit-image) (2.34.2) Requirement already satisfied: tifffile>=2022.8.12 in c:\users\abhineswari madan\anaconda\envs\tensorflowgpu\lib\site-packages (from scikit-image) (2024.7.24) Requirement already satisfied: packaging>=21 in c:\users\abhineswari madan\anaconda\envs\tensorflowgpu\lib\site-packages (from scikit-image) (24.1) Requirement already satisfied: lazy-loader>=0.4 in c:\users\abhineswari madan\anaconda\envs\tensorflowgpu\lib\site-packages (from scikit-image) (0.4) Note: you may need to restart the kernel to use updated packages.
In [17]:
# Convert the image to HSV color space
hsv = cv2.cvtColor(img4, cv2.COLOR_BGR2HSV)
# Defining color range for segmentation
lower_color = (0, 120, 70)
# Lower bound in HSV
upper_color = (10, 255, 255)
# Upper bound in HSV
# Apply color thresholding
mask = cv2.inRange(hsv, lower_color, upper_color)
kernel = cv2.getStructuringElement(cv2.MORPH_ELLIPSE, (5, 5))
cleaned_mask = cv2.morphologyEx(mask, cv2.MORPH_CLOSE, kernel)
cleaned_mask = cv2.morphologyEx(cleaned_mask, cv2.MORPH_OPEN, kernel)
2.extract features: calculate the shape, color and texture features for eah segmented object (e.g area, perimeter, mean color, texture metrics)¶
In [18]:
# Find contours in the cleaned mask
contours, _ = cv2.findContours(cleaned_mask, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
def extract_features(contour, image):
# Calculate area and perimeter
area = cv2.contourArea(contour)
perimeter = cv2.arcLength(contour, True)
# Calculate bounding box
x, y, w, h = cv2.boundingRect(contour)
# Calculate mean color within the contour
mask = np.zeros_like(image[:, :, 0]) # Create a mask for the object
cv2.drawContours(mask, [contour], -1, 255, thickness=cv2.FILLED)
mean_color = cv2.mean(img4, mask=mask)[:3] # Get the mean color (BGR)
# Create a mask to extract object region
object_region = cv2.bitwise_and(img4, img4, mask=mask)
# Calculate texture (using mean and standard deviation of gradients)
gray_object = cv2.cvtColor(object_region, cv2.COLOR_BGR2GRAY)
gradient_x = cv2.Sobel(gray_object, cv2.CV_64F, 1, 0, ksize=3)
gradient_y = cv2.Sobel(gray_object, cv2.CV_64F, 0, 1, ksize=3)
texture_mean = np.mean(np.sqrt(gradient_x**2 + gradient_y**2))
texture_stddev = np.std(np.sqrt(gradient_x**2 + gradient_y**2))
return {
'Area': area,
'Perimeter': perimeter,
'Bounding Box': (x, y, w, h),
'Mean Color': mean_color,
'Texture Mean': texture_mean,
'Texture StdDev': texture_stddev
}
In [19]:
# Extract features for each contour
features = [extract_features(cnt, img4) for cnt in contours]
# Print features for each object
for i, feature in enumerate(features):
print(f"Object {i+1}:")
print(f" Area: {feature['Area']:.2f}")
print(f" Perimeter: {feature['Perimeter']:.2f}")
print(f" Bounding Box: {feature['Bounding Box']}")
print(f" Mean Color (BGR): {feature['Mean Color']}")
print(f" Texture Mean: {feature['Texture Mean']:.2f}")
print(f" Texture StdDev: {feature['Texture StdDev']:.2f}")
print()
Object 1: Area: 10.00 Perimeter: 13.66 Bounding Box: (972, 468, 5, 5) Mean Color (BGR): (72.11764705882352, 95.35294117647058, 172.2941176470588) Texture Mean: 0.02 Texture StdDev: 2.51 Object 2: Area: 65.00 Perimeter: 34.14 Bounding Box: (933, 451, 9, 13) Mean Color (BGR): (16.666666666666664, 28.839506172839506, 92.04938271604938) Texture Mean: 0.02 Texture StdDev: 1.57 Object 3: Area: 86.00 Perimeter: 47.46 Bounding Box: (891, 450, 17, 13) Mean Color (BGR): (38.560747663551396, 42.21495327102804, 90.43925233644859) Texture Mean: 0.02 Texture StdDev: 2.17 Object 4: Area: 73.00 Perimeter: 54.28 Bounding Box: (965, 448, 13, 18) Mean Color (BGR): (23.824742268041238, 40.90721649484536, 143.38144329896906) Texture Mean: 0.04 Texture StdDev: 3.18 Object 5: Area: 10.00 Perimeter: 13.66 Bounding Box: (958, 444, 5, 5) Mean Color (BGR): (51.35294117647059, 64.94117647058823, 117.76470588235294) Texture Mean: 0.01 Texture StdDev: 1.82 Object 6: Area: 805.00 Perimeter: 153.20 Bounding Box: (379, 427, 58, 28) Mean Color (BGR): (14.601607347876005, 44.278989667049366, 120.93340987370838) Texture Mean: 0.14 Texture StdDev: 5.61 Object 7: Area: 112.00 Perimeter: 68.77 Bounding Box: (506, 411, 18, 19) Mean Color (BGR): (62.49295774647887, 115.19718309859155, 219.34507042253523) Texture Mean: 0.08 Texture StdDev: 6.22 Object 8: Area: 10.00 Perimeter: 13.66 Bounding Box: (525, 402, 5, 5) Mean Color (BGR): (60.17647058823529, 110.70588235294117, 203.23529411764704) Texture Mean: 0.02 Texture StdDev: 2.84 Object 9: Area: 28.00 Perimeter: 21.31 Bounding Box: (119, 389, 7, 8) Mean Color (BGR): (6.81578947368421, 9.394736842105262, 121.5) Texture Mean: 0.01 Texture StdDev: 1.12 Object 10: Area: 10.00 Perimeter: 13.66 Bounding Box: (538, 370, 5, 5) Mean Color (BGR): (10.941176470588236, 41.588235294117645, 114.82352941176471) Texture Mean: 0.01 Texture StdDev: 1.33 Object 11: Area: 170.00 Perimeter: 64.28 Bounding Box: (552, 365, 24, 13) Mean Color (BGR): (52.7286432160804, 100.87437185929649, 195.06532663316582) Texture Mean: 0.08 Texture StdDev: 5.69 Object 12: Area: 85.50 Perimeter: 45.56 Bounding Box: (564, 332, 17, 9) Mean Color (BGR): (55.83177570093458, 108.14953271028037, 210.61682242990653) Texture Mean: 0.05 Texture StdDev: 5.04 Object 13: Area: 40.00 Perimeter: 28.14 Bounding Box: (382, 306, 10, 8) Mean Color (BGR): (10.943396226415095, 58.60377358490566, 149.71698113207546) Texture Mean: 0.02 Texture StdDev: 2.35 Object 14: Area: 14.00 Perimeter: 15.66 Bounding Box: (841, 303, 5, 6) Mean Color (BGR): (11.045454545454545, 31.227272727272727, 73.54545454545455) Texture Mean: 0.01 Texture StdDev: 0.94 Object 15: Area: 1502.50 Perimeter: 363.12 Bounding Box: (340, 302, 41, 117) Mean Color (BGR): (17.50120481927711, 46.6777108433735, 108.15783132530122) Texture Mean: 0.22 Texture StdDev: 7.03 Object 16: Area: 113.50 Perimeter: 55.70 Bounding Box: (554, 299, 22, 14) Mean Color (BGR): (77.26086956521739, 123.52173913043478, 219.22463768115944) Texture Mean: 0.08 Texture StdDev: 6.20 Object 17: Area: 41.50 Perimeter: 25.90 Bounding Box: (389, 293, 8, 9) Mean Color (BGR): (71.07407407407408, 126.64814814814814, 238.4074074074074) Texture Mean: 0.04 Texture StdDev: 4.42 Object 18: Area: 90.00 Perimeter: 42.97 Bounding Box: (524, 274, 9, 16) Mean Color (BGR): (63.236363636363635, 127.58181818181818, 248.79999999999998) Texture Mean: 0.06 Texture StdDev: 5.73 Object 19: Area: 67.00 Perimeter: 32.97 Bounding Box: (372, 269, 10, 12) Mean Color (BGR): (8.073170731707318, 41.79268292682927, 137.1341463414634) Texture Mean: 0.02 Texture StdDev: 2.41 Object 20: Area: 189.00 Perimeter: 65.11 Bounding Box: (499, 260, 16, 22) Mean Color (BGR): (14.128440366972479, 66.39908256880734, 173.03211009174314) Texture Mean: 0.06 Texture StdDev: 4.32 Object 21: Area: 17.50 Perimeter: 17.07 Bounding Box: (1295, 230, 6, 6) Mean Color (BGR): (55.69230769230769, 72.61538461538461, 109.34615384615385) Texture Mean: 0.01 Texture StdDev: 1.93 Object 22: Area: 121.50 Perimeter: 55.70 Bounding Box: (907, 223, 16, 20) Mean Color (BGR): (11.712328767123287, 11.705479452054794, 136.92465753424656) Texture Mean: 0.02 Texture StdDev: 2.03 Object 23: Area: 20.50 Perimeter: 19.07 Bounding Box: (1275, 221, 7, 6) Mean Color (BGR): (51.0, 64.73333333333333, 97.6) Texture Mean: 0.01 Texture StdDev: 1.81 Object 24: Area: 97.00 Perimeter: 52.63 Bounding Box: (1246, 214, 20, 10) Mean Color (BGR): (45.31404958677686, 60.057851239669425, 89.91735537190083) Texture Mean: 0.03 Texture StdDev: 2.68 Object 25: Area: 426.50 Perimeter: 148.47 Bounding Box: (1165, 211, 56, 26) Mean Color (BGR): (39.53455284552846, 52.28455284552846, 81.01626016260164) Texture Mean: 0.08 Texture StdDev: 4.00 Object 26: Area: 30.00 Perimeter: 26.14 Bounding Box: (898, 211, 10, 8) Mean Color (BGR): (13.547619047619047, 13.833333333333332, 151.45238095238093) Texture Mean: 0.01 Texture StdDev: 1.56 Object 27: Area: 20.50 Perimeter: 19.07 Bounding Box: (880, 198, 7, 6) Mean Color (BGR): (45.766666666666666, 49.0, 157.83333333333334) Texture Mean: 0.02 Texture StdDev: 2.09 Object 28: Area: 362.00 Perimeter: 121.05 Bounding Box: (841, 175, 32, 31) Mean Color (BGR): (64.6086956521739, 83.22222222222221, 128.95652173913044) Texture Mean: 0.09 Texture StdDev: 5.33 Object 29: Area: 89.50 Perimeter: 45.90 Bounding Box: (850, 146, 7, 20) Mean Color (BGR): (51.45535714285714, 67.47321428571428, 101.6875) Texture Mean: 0.03 Texture StdDev: 2.99 Object 30: Area: 26.00 Perimeter: 20.49 Bounding Box: (939, 137, 6, 8) Mean Color (BGR): (65.16666666666666, 95.6111111111111, 154.02777777777777) Texture Mean: 0.02 Texture StdDev: 2.84 Object 31: Area: 17.50 Perimeter: 17.07 Bounding Box: (812, 130, 6, 6) Mean Color (BGR): (33.53846153846154, 45.96153846153847, 74.11538461538461) Texture Mean: 0.01 Texture StdDev: 1.25 Object 32: Area: 74.50 Perimeter: 41.21 Bounding Box: (924, 113, 13, 12) Mean Color (BGR): (56.322580645161295, 60.032258064516135, 150.97849462365593) Texture Mean: 0.03 Texture StdDev: 3.20 Object 33: Area: 170.00 Perimeter: 85.25 Bounding Box: (892, 107, 32, 12) Mean Color (BGR): (56.27536231884058, 66.54106280193237, 127.0) Texture Mean: 0.07 Texture StdDev: 4.54 Object 34: Area: 202.00 Perimeter: 71.94 Bounding Box: (827, 107, 24, 18) Mean Color (BGR): (43.51282051282052, 55.38888888888889, 91.45299145299147) Texture Mean: 0.04 Texture StdDev: 3.03 Object 35: Area: 704.00 Perimeter: 161.05 Bounding Box: (847, 91, 41, 48) Mean Color (BGR): (41.865979381443296, 51.70876288659794, 109.01932989690721) Texture Mean: 0.10 Texture StdDev: 5.02
3. Analyze Features: Display and interpret the extracted features.¶
In [20]:
# Draw contours and bounding boxes on the original image
for i, contour in enumerate(contours):
# Draw contour
cv2.drawContours(img4, [contour], -1, (0, 255, 0), 2)
# Draw bounding box
x, y, w, h = cv2.boundingRect(contour)
cv2.rectangle(img4, (x, y), (x + w, y + h), (255, 0, 0), 2)
# Annotate the image with the feature values
cv2.putText(img4, f"Object {i+1}", (x, y - 10), cv2.FONT_HERSHEY_SIMPLEX, 0.5, (255, 0, 0), 2)
# Display the result
def display_image(title, img4):
cv2.imshow(title, img4)
cv2.waitKey(0)
cv2.destroyAllWindows()
display_image('Segmented Objects with Features', img4)
In [21]:
output = cv2.imread('Screenshot (4).png')
output_rgb = cv2.cvtColor(output, cv2.COLOR_BGR2RGB)
plt.figure(figsize=(20, 10))
plt.imshow(output_rgb)
plt.show()
In [22]:
pip install --upgrade scikit-image
Requirement already satisfied: scikit-image in c:\users\abhineswari madan\anaconda\envs\tensorflowgpu\lib\site-packages (0.24.0) Requirement already satisfied: numpy>=1.23 in c:\users\abhineswari madan\anaconda\envs\tensorflowgpu\lib\site-packages (from scikit-image) (1.26.4) Requirement already satisfied: scipy>=1.9 in c:\users\abhineswari madan\anaconda\envs\tensorflowgpu\lib\site-packages (from scikit-image) (1.14.0) Requirement already satisfied: networkx>=2.8 in c:\users\abhineswari madan\anaconda\envs\tensorflowgpu\lib\site-packages (from scikit-image) (3.3) Requirement already satisfied: pillow>=9.1 in c:\users\abhineswari madan\anaconda\envs\tensorflowgpu\lib\site-packages (from scikit-image) (10.4.0) Requirement already satisfied: imageio>=2.33 in c:\users\abhineswari madan\anaconda\envs\tensorflowgpu\lib\site-packages (from scikit-image) (2.34.2) Requirement already satisfied: tifffile>=2022.8.12 in c:\users\abhineswari madan\anaconda\envs\tensorflowgpu\lib\site-packages (from scikit-image) (2024.7.24) Requirement already satisfied: packaging>=21 in c:\users\abhineswari madan\anaconda\envs\tensorflowgpu\lib\site-packages (from scikit-image) (24.1) Requirement already satisfied: lazy-loader>=0.4 in c:\users\abhineswari madan\anaconda\envs\tensorflowgpu\lib\site-packages (from scikit-image) (0.4) Note: you may need to restart the kernel to use updated packages.
TASK - 5: Multi object segmentation and counting¶
1.read the image: load an image containing multiple objects¶
2.preprocess the image: apply the preprocessing techniques such as filtering and color space conversion¶
3.segment objects: use techniques like thresholding, contour detection or clustering to segment the objects¶
4.count objects: identify and count the number of segment objects¶
5.display results: show the original image with the segmented objects highlighted and he count of objects¶
In [24]:
image = cv2.imread('multi_object.png')
image_rgb = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
plt.imshow(image_rgb)
plt.title('Original Image')
plt.axis('off')
plt.show()
# Preprocess the image
# Convert to grayscale
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
# Apply Gaussian Blur to reduce noise
blurred = cv2.GaussianBlur(gray, (5, 5), 0)
In [25]:
# Segment objects using thresholding
# You can use Otsu's thresholding method
ret, thresholded = cv2.threshold(blurred, 0, 255, cv2.THRESH_BINARY_INV + cv2.THRESH_OTSU)
# Find contours
contours, hierarchy = cv2.findContours(thresholded, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
# Count the number of objects
num_objects = len(contours)
print(f'Number of segmented objects: {num_objects}')
# Draw contours on the original image
image_with_contours = image_rgb.copy()
cv2.drawContours(image_with_contours, contours, -1, (255, 0, 0), 2)
Number of segmented objects: 56
Out[25]:
array([[[123, 131, 131],
[123, 131, 131],
[123, 131, 131],
...,
[255, 0, 0],
[255, 0, 0],
[255, 0, 0]],
[[123, 131, 131],
[123, 131, 131],
[122, 131, 131],
...,
[255, 0, 0],
[255, 0, 0],
[255, 0, 0]],
[[122, 131, 131],
[122, 131, 131],
[122, 131, 130],
...,
[255, 0, 0],
[255, 0, 0],
[255, 0, 0]],
...,
[[156, 164, 163],
[156, 163, 163],
[157, 164, 163],
...,
[255, 0, 0],
[255, 0, 0],
[255, 0, 0]],
[[156, 164, 163],
[156, 163, 162],
[156, 163, 162],
...,
[255, 0, 0],
[255, 0, 0],
[255, 0, 0]],
[[156, 163, 162],
[155, 162, 162],
[156, 163, 162],
...,
[255, 0, 0],
[255, 0, 0],
[255, 0, 0]]], dtype=uint8)
In [26]:
# Display results
plt.figure(figsize=(15, 5))
plt.subplot(1, 2, 1)
plt.imshow(thresholded, cmap='gray')
plt.title('Thresholded Image')
plt.axis('off')
plt.subplot(1, 2, 2)
plt.imshow(image_with_contours)
plt.title(f'Segmented Objects (Count: {num_objects})')
plt.axis('off')
plt.show()
In [ ]: